#include "BeamWeapon.h"

BeamWeapon::BeamWeapon(String mName, OgreOde::World *world)
{
	this->mName = mName;
    mWorld = world;
    mSceneMgr = world->getSceneManager();
    mSpace = world->getDefaultSpace();
	m_pBillboardChain = new BillboardChain(500);
}


void BeamWeapon::create_beam()
{
	m_pBillboardChain->setNbChainElements(500);

	for (int i = 0; i < 500; i++)
		m_pBillboardChain->setChainElement(i, 
			BillboardChainElement(Vector3(Math::Sin(i / 100.0f * 2 * Math::PI),
			Math::Cos(i / 100.0f * 2 * Math::PI) , i / 10.0f), 
			1.0f, 
			i / 10.0f, 
			ColourValue(1.0f, 0, 0)));

	m_pBillboardChain->updateBoundingBox();

    beam = mSceneMgr->getRootSceneNode ()->createChildSceneNode (mName);
	std::cout << "Added beam: " << mName << std::endl;
    beam->attachObject(m_pBillboardChain);
    mBody = new OgreOde::Body(mWorld, mName);
    beam->attachObject(mBody);
    Vector3 size(50.0,50.0,50.0);
    mGeom = (OgreOde::Geometry*)new OgreOde::BoxGeometry(size, mWorld, mSpace);
    beam->setScale(size.x * 0.02,size.y * 0.02,size.z * 0.02);
    mGeom->setBody(mBody);

   // mEntity->setUserObject(mGeom);
   // mGeom2->setUserObject(static_cast<CollisionTestedObject*>(this));
    mBody->setPosition(start + translate * Ogre::Vector3(0,0,200));
	mBody->setOrientation(translate);
    mBody->setForce(translate * Ogre::Vector3(0,0,2000));

//	beam = sceneMgr->getRootSceneNode()->createChildSceneNode(this->mName);

	// Add it to the scene

	//beam->setOrientation(translate);
//	beam->attachObject(m_pBillboardChain);
}

void BeamWeapon::update_beam()
{
	Quaternion q = beam->getOrientation();
	beam->translate(translate * Vector3(0, 0, 3));


	updates += 1;
}

void BeamWeapon::set_not_visible()
{
   // beam->setVisible(false);
//    beam->detachObject();
 //  sceneMgr->getRootSceneNode()->detachObject(mName);
	mGeom->~ Geometry();
	//mBody->removeGeometry(mGeom);
	mSceneMgr->getRootSceneNode()->removeAndDestroyChild(mName);

}





void BeamWeapon::update(Real elapsedTime, OIS::Keyboard* input, OIS::Mouse* mouse)
{
	if( input->isKeyDown( OIS::KC_V ) )
	{
		for (int i = 0; i < 500; i++)
			beam->translate(Vector3(0,0,5));
	}

}

void BeamWeapon::set_position(Vector3 start, Quaternion translate)
{
	this->start = start;
	this->translate = translate;
}
